"""
selenium详细教程参考： https://zhuanlan.zhihu.com/p/111859925
"""

from time import sleep
import time
from selenium.webdriver import ActionChains
from msedge.selenium_tools import EdgeOptions
from selenium.webdriver.edge.options import Options
from msedge.selenium_tools import Edge
from PIL import Image
from cy import get_code
import scrapy

edge_opt = EdgeOptions()
# 实现规避检测
edge_opt.add_experimental_option("excludeSwitches", ["enable-automation"])

bro = Edge(executable_path='D:\edgedriver\msedgedriver.exe', options=edge_opt)
bro.get(
    'https://cas-443.webvpn.hueb.edu.cn/cas/login?service=https%3A%2F%2Fwebvpn.hueb.edu.cn%2Fusers%2Fauth%2Fcas%2Fcallback%3Furl%3Dhttps%253A%252F%252Fwww.hueb.edu.cn%252F')
# 输入学号与密码
hueb_id = "202222450643"
hueb_password = "xxxxxxx"
# 输入瑞格密码
ruige_password = "xxxxxxx"


def start_process():

    sign = input("输入start以开启进程")
    if sign == "start":
        enter_hueb()
    else:
        return 0


def enter_hueb():
    # 定位账号、密码、验证码输入栏

    user_input = bro.find_element_by_id('username')
    password_input = bro.find_element_by_id('password')
    code_input = bro.find_element_by_id('captcha')
    user_input.send_keys(hueb_id)
    password_input.send_keys(hueb_password)
    # 截屏页面并截取验证码位置的图片
    code_img = bro.find_element_by_id('captchaImg')
    bro.save_screenshot('aa.png')
    size = code_img.size
    area = (534, 487, 580 + int(size['width']), 487 + int(size['height']))
    i = Image.open('./aa.png')
    code_img_name = './code.png'
    frame = i.crop(area)
    frame.save(code_img_name)

    code = get_code('./code.png', '1004')['pic_str']
    code_input.send_keys(code)
    login_button = bro.find_element_by_id('login_submit')
    login_button.click()
    print("成功登录鸡毛！！")
    hueb_to_ruige()


def hueb_to_ruige():
    # 进入瑞格
    ruige_into = bro.find_element_by_xpath('/html/body/div[5]/div/ul/li[18]')
    ruige_into.click()
    sleep(5)
    num = bro.window_handles  # 获取当前页的所有句柄
    bro.switch_to.window(num[-1])  # 跳到新的标签页；因为当前只有一个主页面和新的标签页，所以num[1]表示新标签页，num[0]表示主页面
    print("已跳转至锐格登录页")
    enter_ruige()


def enter_ruige():
    # 登录瑞格
    ruigeuser_input = bro.find_element_by_id('loginMail')
    ruigepswd_input = bro.find_element_by_id('loginPass')
    ruigecode_input = bro.find_element_by_id('authcode')
    ruigeuser_input.send_keys(hueb_id)
    ruigepswd_input.send_keys(ruige_password)
    bro.save_screenshot('bb.png')
    ruigerangle = (1035, 542, 1169, 588)  # 1330 689 136 60
    iruige = Image.open('./bb.png')
    ruigecode_img_name = './ruigecode.png'
    ruigeframe = iruige.crop(ruigerangle)
    ruigeframe.save(ruigecode_img_name)
    ruigecode = get_code('./ruigecode.png', '4004')['pic_str']
    ruigecode_input.send_keys(ruigecode)
    ruigelog_button = bro.find_element_by_id('loginSubmit')
    ruigelog_button.click()
    sleep(3)
    print("成功登录锐格！")
    # find_topic()


def find_topic():
    with open('./text.txt', encoding='utf-8', errors='ignore') as f:
        content = f.read().split('·')
        # 进入瑞格后找到题目位置

        class_into = bro.find_element_by_xpath('//div[@id="outter"]/div[2]/div[2]/div[3]/div/table/tbody/tr/td[1]')
        class_into.click()
        prac_into = bro.find_element_by_xpath('//*[@id="outter"]/div[1]/div/div[2]/ul/li[3]')
        prac_into.click()
        sleep(1)

        swich1 = bro.find_element_by_id('treeDemo_1_switch')
        swich1.click()
        sleep(1)
        swich2 = bro.find_element_by_id('treeDemo_7_switch')
        swich2.click()
        sleep(1)
        swich3 = bro.find_element_by_id('treeDemo_9_switch')
        swich3.click()
        sleep(1)
        for i in range(43,53):
            swich4 = bro.find_element_by_id(f'treeDemo_{i}_a')
            swich4.click()
            sleep(1)
            inputspace = bro.find_element_by_id('myCoder_1')

            sleep(1)
            inputspace.clear()


            con = content[58-i-5]
            con = con.replace('"#', '#').replace('}"', '}').replace('""','"')

            inputspace.send_keys(con)
            submit_btn = bro.find_element_by_id('exerSubBtn2')
            submit_btn.click()
            sleep(3)


enter_hueb()
